package com.bibimaya.auto.mapper.dao.utils;

import com.bibimaya.auto.mapper.dao.constant.Const;

import java.util.List;

/**
 * SelectSqlUtil
 * Created by lijianwei
 */
public class SelectSqlUtil {

    public static String getByIdSql(EntityMap entityMap) {
        FieldMap pk = entityMap.getPrimaryKeyField();
        StringBuffer sql = new StringBuffer();
        sql.append("<script>");
        sql.append(Const.NEW_LINE);
        sql.append(Const.TAB).append("select ");
        sql.append(Const.NEW_LINE);
        sql.append(allColumns(entityMap.getFields(), Const.TAB + Const.TAB));
        sql.append(Const.NEW_LINE);
        sql.append(Const.TAB).append(Const.TAB).append("from ").append(entityMap.getTable());
        sql.append(Const.NEW_LINE);
        sql.append(Const.TAB).append("where ");
        sql.append(pk.getColumn()).append("=#{").append(pk.getField()).append("}");
        sql.append(Const.NEW_LINE);
        sql.append(Const.TAB).append("limit 1 ");
        sql.append(Const.NEW_LINE);
        sql.append("</script>");
        return sql.toString();
    }

    public static String getListSql(Class classEntity){
        return getListSql(EntityMap.newInstance(classEntity, null), null);
    }

    public static String getListSql(EntityMap entityMap){
        return getListSql(entityMap, null);
    }

    public static String getListSql(EntityMap entityMap, String whereSqlAppend) {
        StringBuffer sql = new StringBuffer("<script>");
        sql.append(Const.NEW_LINE);
        sql.append(Const.TAB).append("select");
        sql.append(Const.NEW_LINE);
        sql.append(allColumns(entityMap.getFields(), Const.TAB + Const.TAB));
        sql.append(Const.NEW_LINE);
        sql.append(Const.TAB).append(Const.TAB).append("from ").append(entityMap.getTable());
        sql.append(Const.NEW_LINE);
        sql.append(WhereSqlUtil.whereSql(entityMap, Const.TAB, whereSqlAppend));
        sql.append(orderBySql(Const.TAB));
        sql.append(Const.NEW_LINE);
        sql.append("</script>");
        return sql.toString();
    }

    public static String getCountSql(Class entityClass){
        return getCountSql(EntityMap.newInstance(entityClass, null), null);
    }

    public static String getCountSql(Class entityClass, String suffix, String whereSqlAppend){
        return getCountSql(EntityMap.newInstance(entityClass, suffix), whereSqlAppend);
    }

    public static String getCountSql(EntityMap entityMap){
        return getCountSql(entityMap, null);
    }

    public static String getCountSql(EntityMap entityMap, String whereSqlAppend) {
        StringBuffer sql = new StringBuffer();
        sql.append("<script>");
        sql.append(Const.NEW_LINE);
        sql.append(Const.TAB).append("select ").append("count(1)");
        sql.append(" from ").append(entityMap.getTable());
        sql.append(Const.NEW_LINE);
        sql.append(WhereSqlUtil.whereSql(entityMap, Const.TAB, whereSqlAppend));
        sql.append("</script>");
        return sql.toString();
    }

    public static String getIdListByFieldsSql(Class entityClass){
        return getIdListByFieldsSql(EntityMap.newInstance(entityClass, null));
    }

    public static String getIdListByFieldsSql(Class entityClass, String suffix){
        return getIdListByFieldsSql(EntityMap.newInstance(entityClass, suffix), null);
    }

    public static String getIdListByFieldsSql(EntityMap entityMap){
        return getIdListByFieldsSql(entityMap, null);
    }

    public static String getIdListByFieldsSql(EntityMap entityMap, String whereSqlAppend) {
        StringBuffer sql = new StringBuffer();
        sql.append("<script>");
        sql.append(Const.NEW_LINE);
        sql.append(Const.TAB).append("select ").append(entityMap.getPrimaryKeyField().getField()).append(" ");
        sql.append("from ").append(entityMap.getTable());
        sql.append(Const.NEW_LINE);
        sql.append(WhereSqlUtil.whereSql(entityMap, Const.TAB, whereSqlAppend));
        sql.append("</script>");
        return sql.toString();
    }

    public static String allColumns(List<FieldMap> fieldMaps, String tab){
        StringBuffer sql = new StringBuffer();
        String tabTmp = tab == null ? Const.TAB : tab;
        for (int i = 0; i < fieldMaps.size(); i++) {
            FieldMap fm = fieldMaps.get(i);
            sql.append(tabTmp).append("`"+fm.getColumn()+"`");
            if (i < fieldMaps.size() - 1) {
                sql.append(",");
                sql.append(Const.NEW_LINE);
            }
        }
        return sql.toString();
    }

    public static String orderBySql(String tab){
        StringBuffer orderSql = new StringBuffer();
        orderSql.append(tab).append("<if test='ov != null and ov.sortFields != null'>");
        orderSql.append(Const.NEW_LINE);
        orderSql.append(tab).append(Const.TAB).append("order by ${ov.sortFields} ");
        orderSql.append(Const.NEW_LINE);
        orderSql.append(tab).append("</if>");
        return orderSql.toString();
    }
}
